package com.cn.wanxi.servlet.product;

import com.alibaba.fastjson.JSONObject;
import com.cn.wanxi.model.PageModel;
import com.cn.wanxi.model.ProductModel;
import com.cn.wanxi.service.IProductService;
import com.cn.wanxi.service.impl.ProductServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/back/product/findAll")
public class ProductFindAllServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String productName = req.getParameter("productName");
        String startNormalPrice = req.getParameter("startNormalPrice");
        String endNormalPrice = req.getParameter("endNormalPrice");
        String startPrice = req.getParameter("startPrice");

        ProductModel productModel = new ProductModel();
        productModel.setName(productName == null ? "" : productName);
        productModel.setStartNormalPrice(startNormalPrice == null || "".equals(startNormalPrice) ? 0.0 : Double.parseDouble(startNormalPrice));
        productModel.setEndNormalPrice(endNormalPrice == null || "".equals(endNormalPrice) ? 0.0 : Double.parseDouble(endNormalPrice));


        String curPage = req.getParameter("curPage");
        String pageSize = req.getParameter("pageSize");
        curPage = curPage == null || "".equals(curPage) ? "1" : curPage;
        pageSize = pageSize == null || "".equals(pageSize) ? "10" : pageSize;
        PageModel pageModel = new PageModel();
        pageModel.setCurPage(Integer.parseInt(curPage));
        pageModel.setPageSize(Integer.parseInt(pageSize));
        productModel.setPageModel(pageModel);
        IProductService iProductService = new ProductServiceImpl();

        List<ProductModel> list = iProductService.findAll(productModel);

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("list", list);


        int count = iProductService.getCount(productModel);
        jsonObject.put("count", count);
        resp.getWriter().println(jsonObject);
    }
}
